package com.sunday.scoremanager.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.base.BaseApp;
import com.sunday.scoremanager.entity.StaffAccount;
import com.sunday.scoremanager.http.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/10/26.
 */

public class AddAccountActivity extends BaseActivity {

    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.edit_account)
    ClearEditText editAccount;
    @Bind(R.id.edit_pwd)
    ClearEditText editPwd;
    @Bind(R.id.btn_submit)
    TextView btnSubmit;

    private StaffAccount staffAccount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_account);
        ButterKnife.bind(this);
        staffAccount = (StaffAccount) getIntent().getSerializableExtra("account");
        if (staffAccount == null) {
            titleView.setText("添加账号");
        } else {
            titleView.setText("修改子账号密码");
            editAccount.setEnabled(false);
            editAccount.setFocusable(false);
            editAccount.setText(staffAccount.getUserName());
        }


    }

    private String account, pwd;

    @OnClick(R.id.btn_submit)
    void submit() {
        if (staffAccount == null) {
            account = editAccount.getText().toString();
            pwd = editPwd.getText().toString();
            if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(account)) {
                ToastUtils.showToast(mContext, "内容不能为空");
                return;
            }
            Call<ResultDO> call = ApiClient.getApiAdapter().addAccount(BaseApp.getInstance().getMember().getSellerId(), account, pwd);
            call.enqueue(new Callback<ResultDO>() {
                @Override
                public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                    if (isFinish || response.body() == null) {
                        return;
                    }
                    if (response.body().getCode() == 0) {
                        ToastUtils.showToast(mContext, "添加成功");
                        finish();
                    }
                }

                @Override
                public void onFailure(Call<ResultDO> call, Throwable t) {

                }
            });
        } else {
            pwd = editPwd.getText().toString();
            if (TextUtils.isEmpty(pwd)) {
                ToastUtils.showToast(mContext, "密码不能为空");
                return;
            }
            Call<ResultDO> call = ApiClient.getApiAdapter().updateAccount(staffAccount.getId(), pwd);
            call.enqueue(new Callback<ResultDO>() {
                @Override
                public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                    if (isFinish || response.body() == null) {
                        return;
                    }
                    if (response.body().getCode() == 0) {
                        ToastUtils.showToast(mContext, "修改成功");
                        finish();
                    }
                }

                @Override
                public void onFailure(Call<ResultDO> call, Throwable t) {

                }
            });

        }
    }


}
